<?php include("../data/mysql.class.php"); ?>
<?php
date_default_timezone_set("PRC");
$is_act=isset($_POST["act"]);
if($is_act){$act=$_POST["act"];}else{$act=$_GET["act"];}
$code=400;
$desc="参数有误";
$timestamp=time();
$data=array();

if(isset($act)){
    switch($act){
        case "loginto":
            if(isset($_POST["mobile"])){$usrName =trim($_POST["mobile"]);}else{$usrName="";}
            if(isset($_POST["psw"])){$usrPwd =trim($_POST["psw"]);}else{$usrPwd="";}
            $sql2 = "select * from d_guest where g_name ='$usrName'";
            $conn2=new Mysql();
            $is_u2= $conn2->query($sql2);
            if($is_u2){
                $rs=$conn2->getOne($sql2);
                $rspwd=substr(md5($usrPwd),8,16);
                if($rs["g_psw"]==$rspwd){
                   echo "200";
                }else{
                    echo "201";
                }
            }else{
                echo "500";

            }
            break;
        case "register":
            if(isset($_POST["mobile"])){$usrName =trim($_POST["mobile"]);}else{$usrName="";$status=1;}
            if(isset($_POST["psw"])){$usrPwd =trim($_POST["psw"]);
                $usrPwd=substr(md5($usrPwd),8,16);
            }else{$usrPwd="";}
            if(isset($_POST["email"])){$userEmail =trim($_POST["email"]);}else{$userEmail="";}
            if($status==1){
                echo "500";
            }else{
                $sql="insert into d_guest(g_name,g_psw,g_email,adate) values('$usrName','$usrPwd','$userEmail','".now()."')";
                if($conn->query($sql)){
                    echo "200";
                }else{
                    echo "500";
                }
            }

            break;
        case "joincart":
            //wid,pid
            if(isset($_POST["wid"])){
                if(isset($_POST["pid"])){
                    $wid=$_POST["wid"];
                    $pid=$_POST["pid"];
                    $gopenid=$_POST["openid"];
                    $conc=new Mysql();
                    $sql="select p_price from d_product where id=".$pid;
                    $rs=$conc->getRow($sql);
                    if($rs){
                        $rsGuest=$conc->getOne("select id from d_guest where g_wxid='".$gopenid."'");
                        $gid_id=$rsGuest[0];
                        $pmoney=$rs["p_price"];
                        $o_formid=checkFormid($gid_id);
                        $isnone=$conc->getRow("select id from d_order where o_formid='".$o_formid."'");
                        if($isnone){
                            $o_formid=checkFormid2();
                        }

                        $conc=new Mysql();
                        $sql="insert into d_order(o_formid,o_pid,o_price,o_wid,o_gid,o_paystate,istate,adate) values('".$o_formid."',".$pid.",'".$pmoney."',".$wid.",".$gid_id.",0,0,'". date('Y-m-d H:i:s')."')";
                        if($conc->query($sql)){
                            $code="200";
                            $desc="下单成功,跳转到支付页面";
                            $data["out_trade_no"]=$o_formid;
                            $data["total_fee"]=$pmoney;
                        }else{
                            $code="500";
                            $desc="系统错误,无法下单";

                        }
                    }else{
                        $code="300";
                        $desc="产品不存在";
                    }

                }else{
                    $code="400";
                    $desc="产品编号不得为空!";
                }
            }else{
                $code="400";
                $desc="员工编号参数不得为空!";

            }
            break;
        case "weixinPay":
            include "WeixinPay.php";
            if(isset($_POST["openid"])){
                $openid= $_POST['openid'];
                $out_trade_no = $_POST['out_trade_no'];
               // $total_fee = $_POST['fee'];
                $body = "礼物打赏";
                $conp=new Mysql();
                $rowp=$conp->getRow("select o_price from d_order  where o_formid='".$out_trade_no."'");
                $total_fee=$rowp["o_price"];
                $total_fee = floatval($total_fee*100);
                $weixinpay = new WeixinPay($appid,$openid,$mch_id,$key,$out_trade_no,$body,$total_fee);
                $data["wxpay_params"]=$weixinpay->weixinapp();
                $code="200";
                $desc="请求成功";
            }else{
                $code="300";
                $desc="参数错误";
            }
            break;

        default:
            $code="400";
            $desc="参数不存在";
            break;

    }
}else{

    $desc="类型参数有误!";
    $code=400;

}

function checkFormid($user_id){
return date('YmdHis').$user_id;
}
function checkFormid2(){
    return date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);;
}

?>